package com.web.music.portal.mvc;

import java.beans.PropertyEditorSupport;

import com.web.music.model.Genre;
import com.web.music.interfaces.GenreService;

public class GenrePropertyEditor extends PropertyEditorSupport {
	
	private GenreService genreService;

	public GenrePropertyEditor(GenreService genreService) {
		super();
		this.genreService = genreService;
	}

	@Override
	public String getAsText() {
		if (getValue() != null && getValue() instanceof Genre) {
			Genre genre = (Genre) getValue();
			return genre.getName();
		}
		return super.getAsText();
	}

	@Override
	public void setAsText(String text) throws IllegalArgumentException {
		setValue(genreService.getById(new Long(text)));
	}

}
